/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.gen.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.persistence.BaseConstants;
import com.thinkgem.jeesite.common.service.ServiceException;
import com.thinkgem.jeesite.frame.pm.utils.PmParamUtils;
import com.thinkgem.jeesite.modules.gen.GenConstants;
import com.thinkgem.jeesite.modules.gen.dao.GenTableColumnMapper;
import com.thinkgem.jeesite.modules.gen.entity.GenTable;
import com.thinkgem.jeesite.modules.gen.entity.GenTableColumn;
import com.thinkgem.jeesite.modules.gen.util.ModelUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 代码生成-表信息Service
 *
 * @author kongxiangsheng
 * @version 2018-08-16
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class GenTableColumnService extends ServiceImpl<GenTableColumnMapper, GenTableColumn> implements IService<GenTableColumn> {

    /**
     * 根据父表主键查询
     *
     * @param genTableId
     * @return
     */
    public List<GenTableColumn> getByGenTableId(String genTableId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(GenTableColumn.COL_GEN_TABLE_ID, genTableId);
        queryWrapper.orderByAsc(GenTableColumn.COL_SORT);
        return this.list(queryWrapper);
    }

    /**
     * 根据父表主键查询
     *
     * @param genTableId
     * @return
     */
    public boolean deleteByGenTableId(String genTableId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(GenTableColumn.COL_GEN_TABLE_ID, genTableId);
        return this.remove(queryWrapper);
    }

    /**
     * 获取用于生成代码的column对象信息
     *
     * @param genTable
     * @param baseColumns
     * @return
     */
    public List<GenTableColumn> getForGenerate(GenTable genTable, List<String> baseColumns, List<String> baseTreeColumns) {
        List<GenTableColumn> columnList = this.getByGenTableId(genTable.getId());
        List<String> allColumns = Lists.newArrayList();
        for (GenTableColumn genTableColumn : columnList) {
            if (StringUtils.isBlank(genTableColumn.getComments())) {
                genTableColumn.setComments(genTableColumn.getName());
            }
            genTableColumn.setIsBaseColumn(baseColumns.contains(genTableColumn.getName()) ? "true" : "false");
            genTableColumn.setIsNotBaseColumn(baseColumns.contains(genTableColumn.getName()) ? "false" : "true");
            genTableColumn.setIsBaseTreeColumn(baseTreeColumns.contains(genTableColumn.getName()) ? "true" : "false");
            genTableColumn.setIsNotBaseTreeColumn(baseTreeColumns.contains(genTableColumn.getName()) ? "false" : "true");
            genTableColumn.setAnnotationList(ModelUtils.getAnnotationList(genTableColumn));
            genTableColumn.setNameAndComments(genTableColumn.getName() + " : " + genTableColumn.getComments());
            genTableColumn.setJavaNameCapital(StringUtils.capitalize(genTableColumn.getJavaName()));
            genTableColumn.setJavaNameUnCapital(StringUtils.uncapitalize(genTableColumn.getJavaName()));
            genTableColumn.setNameLower(StringUtils.lowerCase(genTableColumn.getName()));
            genTableColumn.setNameUpper(StringUtils.upperCase(genTableColumn.getName()));
            // 所有字段
            allColumns.add(genTableColumn.getName());
        }

        String checkBaseColumnSwitch = PmParamUtils.getPmParamValue(GenConstants.Param.CHECK_BASE_COL_SWITCH);
        if (BaseConstants.DictSwitch.ON.equalsIgnoreCase(checkBaseColumnSwitch) && !allColumns.containsAll(baseColumns)) {
            throw new ServiceException("数据表[" + genTable.getName() + "]中缺少该项目配置的基础字段[" + baseColumns.toString() + "],请检查!");
        }

        return columnList;
    }

}